"""A concept of distributed network model. Highly experimental academic approach. """

__all__ = ['algorithm', 'graph', 'network', 'util']

#_______________________________________________________________________________

import logging
import os

import algorithm
import graph
import network
import util

#_______________________________________________________________________________

def setup_log(debug_level=None, filename=None):
    logger = logging.getLogger('nxchaos')

    if debug_level is None:
        debug_level = os.getenv('PYNXCHAOS_DEBUG_LEVEL')
    
    else:
        os.environ['PYNXCHAOS_DEBUG_LEVEL'] = debug_level

    if debug_level is not None:

        if filename is None:
            filename = os.getenv('PYNXCHAOS_LOG_FILENAME')

        LEVELS = {'debug': logging.DEBUG,
                  'info': logging.INFO,
                  'warning': logging.WARNING,
                  'error': logging.ERROR,
                  'critical': logging.CRITICAL}

        level = LEVELS.get(debug_level, logging.CRITICAL + 10)
        logger.setLevel(level = level)

        try:
            handler = logging.FileHandler(filename)
            
        except:
            handler = logging.StreamHandler()

        fmt = logging.Formatter('%(asctime)s %(levelname)s: %(name)s: %(message)s')
        handler.setFormatter(fmt)
        logger.addHandler(handler)
        
    else:
        class NullHandler(logging.Handler):
            def emit(self, record):
                pass

        logger.addHandler(NullHandler())
